package com.attilax.ioc;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.engine.spi.SessionFactoryImplementor;
//import org.hibernate.jdbc.Work;
//import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
//import org.springframework.orm.hibernate4.SessionFactoryUtils;

import java.util.Properties;

//import org.hibernate.Session;
//import org.hibernate.SessionFactory;












import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import aaaCfg.ConnectionImp;

import com.attilax.Closure;
import com.attilax.acc.AccSrv4game;
import com.attilax.acc.AccSrvAbs;
import com.attilax.db.DBX;
import com.attilax.db.DbService;
import com.attilax.db.DbServiceQ5;
import com.attilax.exception.ExUtil;
import com.attilax.hre.UrlDslParser;
import com.attilax.hre.UrlDslParserV3;
import com.attilax.io.pathx;
import com.attilax.json.AtiJson;
import com.attilax.lang.Closure2;
import com.attilax.lang.Trigger;
import com.attilax.lang.core;
import com.attilax.net.HttpRequest;
import com.attilax.net.requestImp;
import com.attilax.net.websitex;
import com.attilax.order.OrderService;
import com.attilax.order.OrderService4jobus;
import com.attilax.order.RechargeOrderService;
import com.attilax.order.RechargeOrderService4gamepay;
import com.attilax.orm.AtiOrm;
import com.attilax.orm.AtiOrmAbs;
import com.attilax.persistence.DBCfg;
import com.attilax.persistence.DbutilX;
//import com.attilax.db.Hb4JdbcX;
//import com.attilax.lang.Closure2;
import com.attilax.persistence.Hbx;
import com.attilax.persistence.HbxX;
import com.attilax.persistence.PX;
import com.attilax.sms.Sms1xinxi;
import com.attilax.sms.SmsService;
import com.attilax.spri.SpringUtil;
import com.attilax.sql.SqlExeService;
import com.attilax.sql.SqlExeSrv4pay;
import com.attilax.user.UserService;
import com.attilax.user.UserServiceAbs;
import com.attilax.user.UserSrvImpDistribVer;
import com.attilax.util.HibernateSessionFactory;
import com.attilax.util.PropX;
import com.attilax.web.UrlDslParserIntr;
import com.csmy.my.center.service.UserInfoService;
//import com.focustar.entity.TMbWeixinuser;
//import com.focustar.util.BaseImpl;
//import com.focusx.dao.BranchManagerDao;
//import com.focusx.dao.impl.BranchManagerDaoImpl;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.name.Names;

/**
 * com.attilax.ioc.Ioc4pay
 * 
 * @author Administrator
 *
 */
public class Ioc4pay {
	
	public static void main(String[] args) {
		System.setProperty("cfgfile", "pay.ini");
		System.out.println("a4aa");
		DBCfg bean = com.attilax.ioc.Ioc4pay
 				.getBean(DBCfg.class);
		
		System.out.println(AtiJson.toJson(bean));
//		System.out.println(com.attilax.ioc.Ioc4pay
// 				.getBean(AtiOrmAbs.class));
//		
// 		System.out.println(com.attilax.ioc.Ioc4pay
// 				.getBean(RechargeOrderService.class));
//		System.out.println(com.attilax.ioc.Ioc4pay
//				.getBean(UserServiceAbs.class));
		
		// DBX dx = IocX.getBean(DBX.class);
		// System.out.println(core.toJsonStrO88(dx
		// .execSql("select * from timer_tab limit 5 ")));

		// SpringUtil sprx=(SpringUtil) IocX4jobus.ini().get("sprx");

		// UrlDslParser sprx=IocX4jobus.getBean(UrlDslParser.class);;
		// sprx.exe(new requestImp());
		// //sprx.findBySql("selct * frfom xxx");
		// System.out.println(sprx);
		// System.out.println("");
	}
	static Injector Injector1;
	// public static void main(String[] args) {
	// System.out.println(getCfgVal("hre_web_url"));
	//
	// }
	public static String dbcfg_file;

	public static String getCfgVal(String string) {
		String apptype = System.getProperty("apptype");
		String cfgpath = pathx.webAppPath() + "/" + apptype + ".cfg.txt";
		PropX px = new PropX(cfgpath);
		return px.getProperty(string);
	}

	public static Map<String, Object> map = new HashMap();
	{
		// map.put("copyright-trigger", new Trigger() {
		//
		// @Override
		// public Object exec(Object object) {
		// String fildId = (String) object;
		// String url = "http://120.24.61.254/efilm/a/film/";
		// url = url
		// +
		// "filmMaster/submitOrder?totalPrice=200&filmId=1&houseId=2&ShopId=23&key=keikdunkdfydfhhkdk343rere";
		// System.out.println(url);
		// String[] split = url.split("\\?");
		// String param = split[1];
		// String urlOnly = split[0];
		// String r = HttpRequest.sendPost(urlOnly, param);
		// System.out.println(r);
		// return r;
		// }
		//
		// });
		// map.put("check-playCode-trigger", new Trigger() {
		//
		// @Override
		// public Object exec(Object object) {
		// String fildId = (String) object;
		// String cfgPath = Ati4vod.getCfg();
		// String url_inner = PropX.getConfig(cfgPath, "auth-url_innet");
		// // String playserver = PropX.getConfig(cfgPath, "playserver");
		// if (url_inner.contains("%app-path%"))
		// url_inner = url_inner.replaceAll("%app-path%",
		// PropX.getConfig(cfgPath, "app-path"));
		// String authcode_inner = websitex.WebpageContent(url_inner);
		// String url_outer = PropX.getConfig(cfgPath, "auth-url_outer");
		// url_outer = url_outer + authcode_inner;
		// String rezult = websitex.WebpageContent(url_outer);
		// if (rezult.trim().equals("t"))
		// return true;
		// else
		// return false;
		// }
		//
		// });

	}

	public static Map ini() {
		// map.put("getUserinfoFrmWechatHandler", new
		// Closure<TMbWeixinuser,Object>(){
		//
		// @Override
		// public Object execute(TMbWeixinuser arg0)
		// throws Exception {
		// ExtWebsiteX x=new ExtWebsiteX();
		// x.getUserinfoFrmWechatHandler(arg0);
		// return null;
		// }});
		try {
			map.put("sprx", new Closure() {

				@Override
				public Object execute(Object arg0) throws Exception {
					String p = pathx.classPath() + "/";
					SpringUtil.locations = new String[] { p
							+ "applicationContext.xml" };
					return new SpringUtil();
				}
			}.execute(null));
		} catch (Exception e) {

			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return map;

	}



	/**
	 * @author attilax 老哇的爪�?
	 * @since o92 j_48_40
	 * 
	 * @param class1
	 * @return
	 */
	public static <t> t getBean(Class<t> class1) {
		// attilax 老哇的爪�? j_48_40 o92
		try {
			dbcfg_file = pathx.webAppPath() + "/"
					+ System.getProperty("cfgfile");
			if (Injector1 == null) // ini singleon patter..
			{
				Module module = new Module() {

					@Override
					public void configure(Binder Binder1) {

						AnnotatedBindingBuilder<DBCfg> bind = Binder1
								.bind(DBCfg.class);
						DBCfg executeRzt = getDbcfg().execute(null);
						bind.toInstance(executeRzt);

//						Binder1.bind(DBX.class).to(DbutilX.class);
 					Binder1.bind(SqlExeService.class).to(SqlExeSrv4pay.class);
 					Binder1.bind(RechargeOrderService.class).to(RechargeOrderService4gamepay.class);
 					Binder1.bind(AccSrvAbs.class).to(AccSrv4game.class);
//						Binder1.bind(UrlDslParser.class).to(UrlDslParserV3.class);
// 
//
//						Binder1.bindConstant().annotatedWith(Names.named("thql"))
//								.to(" from TUserUsers ");
					}
				};
				Injector1 = Guice.createInjector(module);
			}
			// Injector[bindings=[ProviderInstanceBinding[key=Key[type=com.google.inject.Injector,
			// annotation=[none]], source=[unknown source],
			// scope=Scopes.NO_SCOPE, provider=Provider<Injector>],
			// ProviderInstanceBinding[key=Key[type=java.util.logging.Logger,
			// annotation=[none]], source=[unknown source],
			// scope=Scopes.NO_SCOPE, provider=Provider<Logger>],
			// InstanceBinding[key=Key[type=com.google.inject.Stage,
			// annotation=[none]], source=[unknown source],
			// instance=DEVELOPMENT],
			// InstanceBinding[key=Key[type=com.attilax.persistence.DBCfg,
			// annotation=[none]],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:244),
			// instance=com.attilax.persistence.DBCfg@10d68fcd],
			// LinkedKeyBinding[key=Key[type=com.attilax.db.DBX,
			// annotation=[none]],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:248),
			// scope=Scopes.NO_SCOPE,
			// target=Key[type=com.attilax.persistence.DbutilX,
			// annotation=[none]]],
			// LinkedKeyBinding[key=Key[type=java.sql.Connection,
			// annotation=[none]],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:249),
			// scope=Scopes.NO_SCOPE, target=Key[type=aaaCfg.ConnectionImp,
			// annotation=[none]]],
			// LinkedKeyBinding[key=Key[type=com.attilax.hre.UrlDslParser,
			// annotation=[none]],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:250),
			// scope=Scopes.NO_SCOPE,
			// target=Key[type=com.attilax.hre.UrlDslParserV3,
			// annotation=[none]]],
			// LinkedKeyBinding[key=Key[type=com.attilax.user.UserService,
			// annotation=[none]],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:251),
			// scope=Scopes.NO_SCOPE,
			// target=Key[type=com.attilax.user.UserSrvImpDistribVer,
			// annotation=[none]]],
			// LinkedKeyBinding[key=Key[type=com.attilax.sms.SmsService,
			// annotation=[none]],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:252),
			// scope=Scopes.NO_SCOPE, target=Key[type=com.attilax.sms.Sms1xinxi,
			// annotation=[none]]],
			// LinkedKeyBinding[key=Key[type=com.attilax.order.OrderService,
			// annotation=[none]],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:253),
			// scope=Scopes.NO_SCOPE,
			// target=Key[type=com.attilax.order.OrderService4jobus,
			// annotation=[none]]],
			// InstanceBinding[key=Key[type=java.lang.String,
			// annotation=@com.google.inject.name.Named(value=thql)],
			// source=com.attilax.ioc.Ioc4pay$2.configure(Ioc4pay.java:369),
			// instance= from TUserUsers ]]]
			t instance = Injector1.getInstance(class1);
			return instance;
		} catch (Exception e) {
			System.out.println("--err::" + e.getMessage() +" getclass:"+class1);
			logger.error(core.getTrace(e));
			ExUtil.throwExV2(e);
		}
		return null;

	}

	public static final Logger logger = LoggerFactory
			.getLogger(IocFacV3_iocx_iocutil.class);

	public static Closure2<Object, DBCfg> getDbcfg() {
		Closure2<Object, DBCfg> closure2 = new Closure2<Object, DBCfg>() {

			@Override
			public DBCfg execute(Object arg0) {
				DBCfg cfg = new DBCfg();
				try {
					// propertyReader pro=new Properties();

					FileInputStream fis = new FileInputStream(new File(
							dbcfg_file));
					Properties properties = new Properties();
					properties.load(fis);
					// pro.
					String url = properties.getProperty("jdbc.url");
					String driver = properties
							.getProperty("jdbc.driverClassName");

					String uname = properties.getProperty("jdbc.username");
					String pwd = properties.getProperty("jdbc.password");
					//
					// configuration.configure(propertyFile);
					// String url =
					// configuration.getProperty("connection.url");
					// String driver = configuration
					// .getProperty("connection.driver_class");
					//
					// String uname =
					// configuration.getProperty("connection.username");
					// String pwd =
					// configuration.getProperty("connection.password");
					// // String url =
					// "jdbc:mysql://@host/@db?zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8";

					cfg.setUrl(url);
					cfg.setUser(uname);
					cfg.setPassword(pwd);
					cfg.setDriver(driver);
					return cfg;
				} catch (Exception e) {
					System.out.println(" wanging:" + e.getMessage());
					e.printStackTrace();
					throw new RuntimeException("cant ini dbcfg", e);
				}

			}
		};
		return closure2;
	}

}
